package com.manor.im.utils;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import com.heytap.msp.push.HeytapPushManager;
import com.heytap.msp.push.callback.ICallBackResultService;
import com.huawei.android.hms.agent.HMSAgent;
import com.huawei.android.hms.agent.common.handler.ConnectHandler;
import com.huawei.android.hms.agent.push.handler.GetTokenHandler;
import com.meizu.cloud.pushsdk.PushManager;
import com.meizu.cloud.pushsdk.util.MzSystemUtils;
import com.manor.common.utils.KLog;
import com.manor.im.ThirdPushTokenMgr;
import com.tencent.imsdk.utils.IMFunc;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.xiaomi.mipush.sdk.MiPushClient;

/**
 * 接入应用
 * 小米
 * 华为
 * 魅族
 * oppo
 * vivo
 * 接入流程
 * 各平台申请appid
 * 集成推送
 * 拿到appid在腾讯im平台添加各平台证书
 */
public class PushUtils {

    private static boolean mIsSupportedBade = true;

    public static void initpush(final Application activity) {
// 在当前工程入口函数，建议在Application的onCreate函数中，添加以下代码

        PushClient.getInstance(activity).initialize();

// 打开push开关, 关闭为turnOffPush，详见api接入文档

        PushClient.getInstance(activity).turnOnPush(new IPushActionListener() {

            @Override

            public void onStateChanged(int state) {

                // TODO: 开关状态处理， 0代表成功
                String regId = PushClient.getInstance(activity).getRegId();

                ThirdPushTokenMgr.getInstance().setThirdPushToken(regId);
                ThirdPushTokenMgr.getInstance().setPushTokenToTIM();

            }

        });
    }


    public static void startPush(final Activity activity) {
        if (IMFunc.isBrandHuawei()) {
            // 华为离线推送
            HMSAgent.connect(activity, new ConnectHandler() {
                @Override
                public void onConnect(int rst) {
                    KLog.i("huaweipush", "huawei push HMS connect end:" + rst);
                }
            });
            getHuaWeiPushToken();
        }
        if (IMFunc.isBrandXiaoMi()) {
            // 小米离线推送
            MiPushClient.registerPush(activity, PrivateConstants.XM_PUSH_APPID, PrivateConstants.XM_PUSH_APPKEY);
        }
        if (IMFunc.isBrandVivo()) {
            // vivo离线推送
            PushClient.getInstance(activity).turnOnPush(new IPushActionListener() {
                @Override
                public void onStateChanged(int state) {
                    if (state == 0) {
                        String regId = PushClient.getInstance(activity).getRegId();
                        KLog.i("vivopush", "vivopush open vivo push success regId = " + regId);
                        ThirdPushTokenMgr.getInstance().setThirdPushToken(regId);
                        ThirdPushTokenMgr.getInstance().setPushTokenToTIM();

                    } else {
                        // 根据vivo推送文档说明，state = 101 表示该vivo机型或者版本不支持vivo推送，链接：https://dev.vivo.com.cn/documentCenter/doc/156
                        KLog.i("vivopush", "vivopush open vivo push fail state = " + state);
                    }
                }
            });
        }
        if (IMFunc.isBrandOppo()) {
            // oppo离线推送  测试版 正式版需要上线oppo平台
            try {
                HeytapPushManager.init(activity, true);
                HeytapPushManager.register(activity, PrivateConstants.OPPO_PUSH_APPKEY, PrivateConstants.OPPO_PUSH_APPSECRET, mPushCallback);//setPushCallback接口也可设置callback
                HeytapPushManager.requestNotificationPermission();
                createNotificationChannel(activity);
                //  HeytapPushManager.openNotificationSettings();
            } catch (Exception e) {
                e.printStackTrace();
                //  TestModeUtil.addLogString(e.getLocalizedMessage());
            }
        }
//         client = new HuaweiApiClient.Builder(this)
//                .addApi(HuaweiPush.PUSH_API)
//                .addConnectionCallbacks(this)
//                .addOnConnectionFailedListener(this)
//                .build();
//        client.connect(MainActivity.this);

    }

    public static void getHuaWeiPushToken() {
        HMSAgent.Push.getToken(new GetTokenHandler() {
            @Override
            public void onResult(int rtnCode) {
                KLog.e("push", "huawei push get token result code: " + rtnCode);
            }
        });

    }






    public static void deleteToken(Context context) {

        if (IMFunc.isBrandHuawei()) {
            HMSAgent.destroy();
        }

        if (IMFunc.isBrandVivo()) {
            PushClient.getInstance(context).turnOffPush(new IPushActionListener() {
                @Override
                public void onStateChanged(int i) {

                }
            });
        }
        if (IMFunc.isBrandOppo()) {
            HeytapPushManager.unRegister();

        }
        if (MzSystemUtils.isBrandMeizu(context)) {
            // 魅族离线推送
            PushManager.unRegister(context, PrivateConstants.MZ_PUSH_APPID, PrivateConstants.MZ_PUSH_APPKEY);
        }
        if (IMFunc.isBrandXiaoMi()) {
            MiPushClient.unregisterPush(context);
        }
    }

    public static void createNotificationChannel(Context context) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "com.taomi.oppopush";//name是名称
            String description = "this is oppo";//描述
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("taomi", name, importance);//id是“taomi”
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public static ICallBackResultService mPushCallback = new ICallBackResultService() {
        @Override
        public void onRegister(int code, String s) {
            if (code == 0) {
                KLog.e("oppopush", "coed= " + code + "s=" + s);
                ThirdPushTokenMgr.getInstance().setThirdPushToken(s);
                ThirdPushTokenMgr.getInstance().setPushTokenToTIM();
                //     showResult("注册成功", "registerId:" + s);
            } else {
                KLog.e("oppopush", "code=" + code + ",msg=" + s);
            }
        }

        @Override
        public void onUnRegister(int code) {
            if (code == 0) {
                KLog.e("oppopush", "code=" + code);
            } else {
                KLog.e("oppopush", "code=" + code);
            }
        }

        @Override
        public void onGetPushStatus(final int code, int status) {
            if (code == 0 && status == 0) {
                KLog.e("oppopush", "code=" + code + ",status=" + status);
            } else {
                KLog.e("oppopush", "code=" + code + ",status=" + status);
            }
        }

        @Override
        public void onGetNotificationStatus(final int code, final int status) {
            if (code == 0 && status == 0) {
                KLog.e("oppopush", "code=" + code + ",status=" + status);
            } else {
                KLog.e("oppopush", "code=" + code + ",status=" + status);
            }
        }

        @Override
        public void onSetPushTime(final int code, final String s) {
            KLog.e("oppopush", "code=" + code + ",result:" + s);
        }

    };

}
